﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using Journal.Common.Helpers.HtmlControls;

namespace Journal.Common.Helpers
{
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString FormInputTextRow<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, bool isValidate)
        {
            TagBuilder container = html.FormContainer(expression, html.TextBoxFor(expression).ToString());
            if (isValidate)
            {
                container.InnerHtml += html.ValidationMessageFor(expression);
            }

            return html.RenderFormRow(expression, container.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputTextRow<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, bool isValidate, object htmlAttributes)
        {
            TagBuilder container = html.FormContainer(expression, html.TextBoxFor(expression, htmlAttributes).ToString());
            return html.RenderFormRow(expression, container.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputPasswordRow<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
        {
            TagBuilder container = html.FormContainer(expression, html.FormInputPassword(htmlAttributes).ToString());
            return html.RenderFormRow(expression, container.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputPasswordRow<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression)
        {
            TagBuilder container = html.FormContainer(expression, html.FormInputPassword(expression).ToString());
            return html.RenderFormRow(expression, container.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputTextMultilineRow<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression)
        {
            TagBuilder container = html.FormContainer(expression, html.TextAreaFor(expression).ToString());
            return html.RenderFormRow(expression, container.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputTextMultilineRow<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
        {
            TagBuilder container = html.FormContainer(expression, html.TextAreaFor(expression, htmlAttributes).ToString());
            return html.RenderFormRow(expression, container.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputDropDownRow<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items)
        {
            TagBuilder container = html.FormContainer(expression, html.DropDownListFor(expression, items).ToString());
            return html.RenderFormRow(expression, container.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputDropDownRow<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items, object htmlAttributes)
        {
            TagBuilder container = html.FormContainer(expression, html.DropDownListFor(expression, items, htmlAttributes).ToString());
            return html.RenderFormRow(expression, container.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString RenderFormRow<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, string formContent)
        {
            string containerContent = string.Concat(html.FormLabel(expression), formContent);
            TagBuilder container = html.FormContainer(expression, containerContent, "formRow");
            return new MvcHtmlString(container.ToString(TagRenderMode.Normal));
        }

        public static TagBuilder FormContainer<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, string containerContent, string cssClass, object htmlAttributes)
        {
            TagBuilder container = html.FormContainerControl(containerContent, cssClass, htmlAttributes);
            return html.FormContainerControl(containerContent, cssClass, htmlAttributes);
        }

        public static TagBuilder FormContainer<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, string containerContent, string cssClass)
        {
            TagBuilder container = html.FormContainerControl(containerContent, cssClass);
            return html.FormContainerControl(containerContent, cssClass);
        }

        public static TagBuilder FormContainer<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, string containerContent)
        {
            TagBuilder container = html.FormContainerControl(containerContent, "dataRow");
            return html.FormContainerControl(containerContent, "dataRow");
        }

       /* public static MvcHtmlString FormInputDropDown<TModel>(this HtmlHelper<TModel> html, IEnumerable<SelectListItem> items, object htmlAttributes = null, IDictionary<string, string> dataParams = null)
        {
            TagBuilder select = new TagBuilder("select");
            select.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            select = InsertDataAttributes(dataParams, select);
            foreach (var item in items)
            {
                TagBuilder option = new TagBuilder("option");
                option.Attributes.Add("value", item.Value);
                option.InnerHtml += item.Text;
                select.InnerHtml += option.ToString(TagRenderMode.Normal);
            }

            return new MvcHtmlString(select.ToString(TagRenderMode.Normal));
        }*/

        public static MvcHtmlString FormLabel<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
        {
            TagBuilder labelField = new TagBuilder("div");
            labelField.AddCssClass("labelRow");
            labelField.InnerHtml += html.LabelFor(expression, new { @class = "labelField" });
            labelField.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return MvcHtmlString.Create(labelField.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormLink(this HtmlHelper html, string value, string url)
        {
            return html.LinkControl(value, url);
        }

        public static MvcHtmlString FormLink(this HtmlHelper html, string value, string url, object htmlAttributes)
        {
            return html.LinkControl(value, url, htmlAttributes);
        }

        public static MvcHtmlString FormLink(this HtmlHelper html, string value, string url, IDictionary<string, string> dataParams)
        {
            return html.LinkControl(value, url, dataParams);
        }

        public static MvcHtmlString FormLink(this HtmlHelper html, string value, string url, object htmlAttributes, IDictionary<string, string> dataParams)
        {
            return html.LinkControl(value, url, htmlAttributes, dataParams);
        }

        public static MvcHtmlString FormImageLink(this HtmlHelper html, string linkUrl, string imageSrc)
        {
            return html.ImageLinkControl(linkUrl, imageSrc);
        }

        public static MvcHtmlString FormImageLink(this HtmlHelper html, string linkUrl, string imageSrc, object htmlAttributes)
        {
            return html.ImageLinkControl(linkUrl, imageSrc, htmlAttributes); ;
        }

/*        public static MvcHtmlString RenderDataParams(TagBuilder element, IDictionary<string, string> dataParams)
        {
            return "";
        }*/
    }
}